package com.practise.rabbitmqdemo.controller;

import com.alibaba.fastjson.JSON;
import com.practise.rabbitmqdemo.config.FanoutRabbitConfig;
import com.practise.rabbitmqdemo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "fanout")
@Api(value = "send fanout Msg controller", tags = {"fanout消息队列接口"})
public class FanoutMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "添加用户到fanout1", httpMethod = "POST")
    @RequestMapping(value = "sendTo")
    public void sendTo(@RequestBody @ApiParam(name = "User", value = "用户信息", required = true) User user) {
        rabbitTemplate.convertAndSend(FanoutRabbitConfig.FANOUT_EXCHANGE,null, JSON.toJSONString(user));
        return;
    }

    @ApiOperation(value = "添加消息到fanout22", httpMethod = "POST")
    @RequestMapping(value = "sendTo2")
    public String sendTo2(String userName, String msg) {
        String concatMsg = "hi,".concat(userName).concat(",").concat(msg);
        rabbitTemplate.convertAndSend(FanoutRabbitConfig.FANOUT_EXCHANGE, "topic.aaa",concatMsg);
        return concatMsg;
    }

}
